{% extends 'base.html' %}
{% load static %}

{% block title %}借用申请管理{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-handshake me-2"></i>借用申请管理</h5>
                    <a href="{% url 'assets:borrow_request_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>申请借用
                    </a>
                </div>
                
                <div class="card-body">
                    <!-- 搜索和筛选 -->
                    <form method="get" class="row g-3 mb-4">
                        <div class="col-md-3">
                            <input type="text" class="form-control" name="search" 
                                   placeholder="搜索资产名称、编号或借用人..." value="{{ search_query }}">
                        </div>
                        <div class="col-md-2">
                            <select class="form-select" name="status">
                                <option value="">全部状态</option>
                                {% for value, label in status_choices %}
                                    <option value="{{ value }}" {% if value == selected_status %}selected{% endif %}>
                                        {{ label }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select class="form-select" name="department">
                                <option value="">全部部门</option>
                                {% for dept in departments %}
                                    <option value="{{ dept.id }}" {% if dept.id|stringformat:"s" == selected_department %}selected{% endif %}>
                                        {{ dept.name }}
                                    </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select class="form-select" name="overdue">
                                <option value="">全部</option>
                                <option value="yes" {% if selected_overdue == 'yes' %}selected{% endif %}>仅逾期</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <button type="submit" class="btn btn-outline-primary me-2">
                                <i class="fas fa-search me-1"></i>搜索
                            </button>
                            <a href="{% url 'assets:overdue_borrows' %}" class="btn btn-outline-warning">
                                <i class="fas fa-exclamation-triangle me-1"></i>逾期提醒
                            </a>
                        </div>
                    </form>

                    <!-- 借用申请列表 -->
                    {% if page_obj %}
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>资产信息</th>
                                        <th>借用人</th>
                                        <th>借用目的</th>
                                        <th>时间信息</th>
                                        <th>状态</th>
                                        <th>审批信息</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for borrow in page_obj %}
                                        <tr {% if borrow.is_overdue %}class="table-warning"{% endif %}>
                                            <td>
                                                <strong>{{ borrow.asset.name }}</strong><br>
                                                <small class="text-muted">{{ borrow.asset.asset_number }}</small><br>
                                                <small class="text-muted">{{ borrow.asset.category.name }}</small>
                                            </td>
                                            <td>
                                                {{ borrow.borrower.get_full_name|default:borrow.borrower.username }}<br>
                                                {% if borrow.borrower_department %}
                                                    <small class="text-muted">{{ borrow.borrower_department.name }}</small>
                                                {% endif %}
                                                {% if borrow.contact_phone %}
                                                    <br><small class="text-muted"><i class="fas fa-phone"></i> {{ borrow.contact_phone }}</small>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <span class="badge bg-info">{{ borrow.get_purpose_display }}</span><br>
                                                <small class="text-muted">{{ borrow.purpose_description|truncatechars:30 }}</small>
                                            </td>
                                            <td>
                                                <small>
                                                    <strong>申请：</strong>{{ borrow.created_at|date:"m-d H:i" }}<br>
                                                    <strong>预计归还：</strong>{{ borrow.expected_return_date|date:"m-d H:i" }}<br>
                                                    {% if borrow.borrowed_at %}
                                                        <strong>借出：</strong>{{ borrow.borrowed_at|date:"m-d H:i" }}<br>
                                                    {% endif %}
                                                    {% if borrow.actual_return_date %}
                                                        <strong>归还：</strong>{{ borrow.actual_return_date|date:"m-d H:i" }}
                                                    {% elif borrow.is_overdue %}
                                                        <span class="text-danger"><strong>逾期 {{ borrow.days_overdue }} 天</strong></span>
                                                    {% endif %}
                                                </small>
                                            </td>
                                            <td>
                                                {% if borrow.status == 'pending' %}
                                                    <span class="badge bg-secondary">{{ borrow.get_status_display }}</span>
                                                {% elif borrow.status == 'approved' %}
                                                    <span class="badge bg-success">{{ borrow.get_status_display }}</span>
                                                {% elif borrow.status == 'borrowed' %}
                                                    <span class="badge bg-primary">{{ borrow.get_status_display }}</span>
                                                {% elif borrow.status == 'returned' %}
                                                    <span class="badge bg-info">{{ borrow.get_status_display }}</span>
                                                {% elif borrow.status == 'overdue' %}
                                                    <span class="badge bg-warning">{{ borrow.get_status_display }}</span>
                                                {% elif borrow.status == 'rejected' %}
                                                    <span class="badge bg-danger">{{ borrow.get_status_display }}</span>
                                                {% else %}
                                                    <span class="badge bg-light text-dark">{{ borrow.get_status_display }}</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if borrow.approver %}
                                                    {{ borrow.approver.get_full_name|default:borrow.approver.username }}<br>
                                                    <small class="text-muted">{{ borrow.approved_at|date:"m-d H:i" }}</small>
                                                    {% if borrow.approval_notes %}
                                                        <br><small class="text-muted">{{ borrow.approval_notes|truncatechars:20 }}</small>
                                                    {% endif %}
                                                {% else %}
                                                    <span class="text-muted">待审批</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <div class="btn-group btn-group-sm" role="group">
                                                    <a href="{% url 'assets:borrow_request_detail' borrow.pk %}" 
                                                       class="btn btn-outline-primary" title="查看详情">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    {% if user.is_staff %}
                                                        {% if borrow.can_approve %}
                                                            <a href="{% url 'assets:borrow_request_approve' borrow.pk %}" 
                                                               class="btn btn-outline-success" title="审批">
                                                                <i class="fas fa-check"></i>
                                                            </a>
                                                        {% elif borrow.can_borrow %}
                                                            <a href="{% url 'assets:borrow_asset_out' borrow.pk %}" 
                                                               class="btn btn-outline-info" title="借出">
                                                                <i class="fas fa-arrow-right"></i>
                                                            </a>
                                                        {% elif borrow.can_return %}
                                                            <a href="{% url 'assets:return_asset' borrow.pk %}" 
                                                               class="btn btn-outline-warning" title="归还">
                                                                <i class="fas fa-arrow-left"></i>
                                                            </a>
                                                        {% endif %}
                                                    {% endif %}
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 分页 -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="借用申请分页">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page=1{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_department %}&department={{ selected_department }}{% endif %}{% if selected_overdue %}&overdue={{ selected_overdue }}{% endif %}">首页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_department %}&department={{ selected_department }}{% endif %}{% if selected_overdue %}&overdue={{ selected_overdue }}{% endif %}">上一页</a>
                                        </li>
                                    {% endif %}

                                    <li class="page-item active">
                                        <span class="page-link">第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</span>
                                    </li>

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_department %}&department={{ selected_department }}{% endif %}{% if selected_overdue %}&overdue={{ selected_overdue }}{% endif %}">下一页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}{% if selected_status %}&status={{ selected_status }}{% endif %}{% if selected_department %}&department={{ selected_department }}{% endif %}{% if selected_overdue %}&overdue={{ selected_overdue }}{% endif %}">末页</a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-handshake fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">暂无借用申请</h5>
                            <p class="text-muted">点击上方按钮创建第一个借用申请</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
